package com.qianseit.westore.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.beautymiracle.androidclient.R;
import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.DoActivity;
import com.qianseit.westore.DoFragment;
import com.qianseit.westore.Run;
import com.qianseit.westore.http.JsonTask;
import com.qianseit.westore.ui.CircleFlowIndicator;
import com.qianseit.westore.ui.FlowView;

public class MainHomeActivity extends DoFragment implements View.OnClickListener{

	private Button[] tabs;
	private FlowView mFlowView;

//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.main_home_activity);
//		mFlowView = (FlowView) findViewById(R.id.main_top_adsview);
//		mFlowView.setAdapter(new FlowAdapter());
//		CircleFlowIndicator mTopAdsIndicator = (CircleFlowIndicator) findViewById(R.id.main_top_adsview_indicator);
//		mFlowView.setFlowIndicator(mTopAdsIndicator);
//		mTopAdsIndicator.setViewFlow(mFlowView);
//
//		tabs = new Button[4];
//		
//		
//		tabs[0] = (Button) findViewById(R.id.main_home_activity_btn1);
//		tabs[1] = (Button) findViewById(R.id.main_home_activity_btn2);
//		tabs[2] = (Button) findViewById(R.id.main_home_activity_btn3);
//		tabs[3] = (Button) findViewById(R.id.main_home_activity_btn4);
//		for (int i = 0; i < tabs.length; i++) {
//			tabs[i].setOnClickListener(this);
//		}
//	}

	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActionBar.setTitle("美丽奇迹商城");
		rootView = inflater.inflate(R.layout.main_home_activity, null);
		mFlowView = (FlowView) rootView.findViewById(R.id.main_top_adsview);
		mFlowView.setAdapter(new FlowAdapter());
		CircleFlowIndicator mTopAdsIndicator = (CircleFlowIndicator) rootView
				.findViewById(R.id.main_top_adsview_indicator);
		mFlowView.setFlowIndicator(mTopAdsIndicator);
		mTopAdsIndicator.setViewFlow(mFlowView);

		tabs = new Button[4];
		tabs[0] = (Button) findViewById(R.id.main_home_activity_btn1);
		tabs[1] = (Button) findViewById(R.id.main_home_activity_btn2);
		tabs[2] = (Button) findViewById(R.id.main_home_activity_btn3);
		tabs[3] = (Button) findViewById(R.id.main_home_activity_btn4);
		for (int i = 0; i < tabs.length; i++) {
			tabs[i].setOnClickListener(this);
		}
		new JsonTask().execute(new AboutFragment.UpdateTask(
				(DoActivity) mActivity));
	}

//	@Override
//	public void onClick(View v) {
//		if (v.getId() == R.id.main_home_activity_btn1) {
//			startActivity(AgentActivity.intentForFragment(this,
//					AgentActivity.FRAGMENT_ALL_GOODS));
//		} else if (v.getId() == R.id.main_home_activity_btn2) {
//			startActivity(AgentActivity.intentForFragment(this,
//					AgentActivity.FRAGMENT_SHOPPING_CAR));
//		} else if (v.getId() == R.id.main_home_activity_btn3) {
//			startActivity(AgentActivity.intentForFragment(this,
//					AgentActivity.FRAGMENT_ABOUT_US));
//		} else if (v.getId() == R.id.main_home_activity_btn4) {
//			startActivity(AgentActivity.intentForFragment(this,
//					AgentActivity.FRAGMENT_ACCOUNT_HOME));
//		}
//	}

	private class FlowAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
//				ImageView view = new ImageView(MainHomeActivity.this);
				ImageView view = new ImageView(mActivity);
				view.setLayoutParams(new AbsListView.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				view.setScaleType(ScaleType.CENTER_CROP);
				view.setOnClickListener(mAdViewClickListener);
				convertView = view;
			}
			if (position == 0) {
				((ImageView) convertView).setImageResource(R.drawable.pnb1);
			} else if (position == 1) {
				((ImageView) convertView).setImageResource(R.drawable.pnb2);
			} else if (position == 2) {
				((ImageView) convertView).setImageResource(R.drawable.pnb3);
			} else if (position == 3) {
				((ImageView) convertView).setImageResource(R.drawable.pnb4);
			}
			return convertView;
		}

	}
	
//	private long mLastBackDownTime = 0;
//	@Override
//	public boolean onKeyDown(int key, KeyEvent e) {
//		Run.log("repeat count:", e.getRepeatCount());
//		if (key == KeyEvent.KEYCODE_BACK && e.getRepeatCount() == 0) {
//			long now = System.currentTimeMillis();
//			// 点击Back键提示退出程序
//			if (now - mLastBackDownTime > 2000) {
//				mLastBackDownTime = now;
//				Run.alert(this, R.string.exit_message);
//			} else {
//				this.finish();
//			}
//			return true;
//		}
//
//		return super.onKeyDown(key, e);
//	}

	private OnClickListener mAdViewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			startActivity(AgentActivity.intentForFragment(mActivity,
					AgentActivity.FRAGMENT_ALL_GOODS));
		}

	};

	@Override
	public void ui(int what, Message msg) {
		// TODO Auto-generated method stub
		
	}

}
